﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class SaveInfo
{
    public int coin;
    public Vector3 pos;
    public bool isForward;
    public int health;
}

public class SaveInfoView : MonoBehaviour
{
    public Image sprite;
    public Button start,clear;
    public Text text;
    SaveGameData saveInfo;

    private void Awake()
    {
        start.onClick.AddListener(StartNewGame);
        if (clear != null)
        {
            clear.onClick.AddListener(ClearSave);
        }
    }
    public void Init(string name,SaveGameData info)
    {
        text.text = name;
        saveInfo = info;
    }

    private void ClearSave()
    {
        SaveGame.ClearSave(saveInfo);
        saveInfo = null;
        Destroy(this.gameObject);
    }
    
    private void StartNewGame()
    {
        string sceneName = "Level1";
        if (saveInfo!=null)
        {
            sceneName = SaveGame.GetSceneName(saveInfo.title);
        }
        GameManager.Instance.AsyncLoadScene(sceneName, LoadSceneComplete);
        UIManager.Instance.selectSavePanel.Hide();


    }

    private void LoadSceneComplete()
    {
        GameObject player = GameObject.FindGameObjectWithTag("Player");
        if (!player)
        {
            GameObject p = Resources.Load<GameObject>("Prefabs/Player");
            CharacterInfo info = null;
            if (saveInfo!=null)
            {
                player = Instantiate(p, new Vector3(saveInfo.savePosX, saveInfo.savePosY, 0), Quaternion.identity);
                info=  saveInfo.characterInfo;
            }
            else
            {
                player= Instantiate(p, DefaultData.playerDefultPos, Quaternion.identity);
                info = new CharacterInfo(0, "123", 5, 1, 1);
               
            }
            UIManager.Instance.combatView.BindCharacter(info);
            player.GetComponent<Character>().BindCharacterInfo(info);
            
        }

        Camera.main.GetComponent<CameraController2D>().SetTarget(player.transform);
        UIManager.Instance.setBg(false);
        UIManager.Instance.combatView.Show(); 

        //UIManager.Instance.combatView.Initinalize();
    }
}
